import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/net/api/http_api.dart';
import 'package:ls_work/res/colors.dart';
import 'package:ls_work/res/gaps.dart';
import 'package:ls_work/util/ext_utils.dart';
import 'package:ls_work/util/image_utils.dart';

import '../../viewmodel/food/food_order_record_view_model.dart';

class FoodOrderRecordPage extends BaseStatefulMvvmWidget {
  @override
  State<StatefulWidget> createState() => _FoodOrderRecordState();
}

class _FoodOrderRecordState
    extends BaseMvvmListState<FoodOrderRecordViewModel, FoodOrderRecordPage> {

  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();

  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      key: _scaffoldKey,
      backgroundColor: Color(0xfff1f1f1),
      appBar: AppBar(
        title: Text('点餐记录'),
        actions: [
          IconButton(onPressed: (){
            _scaffoldKey.currentState?.openEndDrawer();
          }, icon: Icon(Icons.filter_list_alt))
        ],
      ),
      endDrawer: SafeArea(child: Column(
        children: [
          CommonText("记录查询",textSize: 18).addToContainer(height: AppBar().preferredSize.height,alignment: Alignment.center),
          Gaps.line,
          IconTitleTextWidget("创建开始时间：",hintText: '选择开始时间',text: viewModel.startDateStr,).onTap(() {
            viewModel.selectStartDate(click: (date){
              viewModel.requestRefresh();
            });
          }),
          IconTitleTextWidget("创建结束时间：",hintText: '选择结束时间',text: viewModel.endDateStr,).onTap(() {
            viewModel.selectEndDate(click: (date){
              viewModel.requestRefresh();
            });
          }),
          IconTitleTextWidget("用餐日期：",hintText: '选择用餐日期',text: viewModel.endDateStr,).onTap(() {
            viewModel.selectUseDate();
          }),
          IconTitleTextWidget("用餐时段：",hintText: '选择用餐时段',text: viewModel.useTime?.pickName??"",).onTap(() {
            viewModel.selectUseTime();
          }),
          IconTitleTextWidget("楼栋：",hintText: '选择楼栋',text: viewModel.selectBuilding?.iName??"",).onTap(() {
            viewModel.selectBuildingPick();
          }),
          IconTitleTextWidget("楼层：",hintText: '选择楼层',text: viewModel.selectFloor?.iName??"",).onTap(() {
            viewModel.selectFloorPick();
          }),
          Container().setWeight(1),
          Gaps.line,
          Row(
            children: [
              TextButton(onPressed: (){
                viewModel.reset();
                Go().pop();
              }, child: CommonText('重置'),
              style: ButtonStyle(
                  padding: WidgetStateProperty.all(EdgeInsets.zero),
                  minimumSize: WidgetStateProperty.all(Size.fromHeight(45)),
                  shape: WidgetStateProperty.all(RoundedRectangleBorder()),
              ),).setWeight(1),
              TextButton(onPressed: (){
                Go().pop();
              }, child: CommonText('确定',textColor: Colors.white),
              style: ButtonStyle(
                padding: WidgetStateProperty.all(EdgeInsets.zero),
                minimumSize: WidgetStateProperty.all(Size.fromHeight(45)),
                shape: WidgetStateProperty.all(RoundedRectangleBorder()),
                backgroundColor: WidgetStateProperty.all(Colors.blue)
              ),).setWeight(1),
            ],
          )
        ],
      )).addToContainer(color: Colors.white,width: 300),
      body: Column(
        children: [
          Row(
            children: [
              CommonInput(
                hintText: '输入长者姓名搜索',
                onChanged: (str) {
                  viewModel.searchName = str;
                },
                onSubmitted: (str) {
                  viewModel.requestRefresh();
                },
                textInputAction: TextInputAction.search,
                border: InputBorder.none,
                isDense: true,
                padding: EdgeInsets.zero,
              ).setWeight(1),
              Icon(Icons.search).onTap(() {
                viewModel.requestRefresh();
              })
            ],
          ).addToContainer(
              decoration: BoxDecoration(
                  color: Colors.white, borderRadius: 4.borderRadius),
              margin: EdgeInsets.symmetric(horizontal: 20, vertical: 16),
              padding: EdgeInsets.symmetric(horizontal: 16, vertical: 10)),
          loadingContentWidget.setWeight(1)
        ],
      ),
    );
  }

  @override
  Widget createItemWidget(int index) {
    var item = viewModel.listItems[index];
    // print("statusName===>${item.statusName}");
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          children: [
            CommonText(item.elderHumanName,
                textSize: 17, fontWeight: FontWeight.bold),
            Container(
              width: 13,
            ),
            CommonText(item.elderHumanRoom ?? "", textSize: 15),
            Container(
              width: 30,
            ),
            CommonText(item.useTimeSectionName, textColor: Colours.app_main).setWeight(1),
            CommonText(item.statusName)
          ],
        ),
        Container(
          height: 6,
        ),
        CommonText(
            item.useDate?.changeTime(format: [yyyy, '-', mm, '-', dd, '  ', D]),
            textSize: 15),
        Container(
          height: 6,
        ),

        CommonText(item.goodsInfo, textSize: 15),

        // item.goodsList.isEmpty
        //     ? Container()
        //     : GridView.count(
        //         shrinkWrap: true,
        //         physics: NeverScrollableScrollPhysics(),
        //         crossAxisCount: 2,
        //         childAspectRatio: 2,
        //         crossAxisSpacing: 10,
        //         mainAxisSpacing: 10,
        //         children: item.goodsList.map((e) {
        //           return Row(
        //             children: [
        //               (e.imgAttachId==null?ImageLoad(
        //                 ImageUtils.getImgPath('none'),
        //                 width: 70,
        //                 height: 70,
        //                 isAsset: true,
        //               ):ImageLoad(
        //                 "${HttpApi.imageHead}${e.imgAttachId}",
        //                 width: 70,
        //                 height: 70,
        //                 fit: BoxFit.cover,
        //               )).toRound(borderRadius: 5.borderRadius),
        //               Container(
        //                 width: 10,
        //               ),
        //               Column(
        //                 crossAxisAlignment: CrossAxisAlignment.start,
        //                 mainAxisAlignment: MainAxisAlignment.spaceAround,
        //                 children: [
        //                   CommonText(e.name),
        //                   Row(
        //                     children: [
        //                       CommonText("x${e.number}").setWeight(1),
        //                       CommonText("¥${e.price*e.number}", textColor: Colors.red),
        //
        //                     ],
        //                   )
        //                 ],
        //               ).setWeight(1)
        //             ],
        //           );
        //         }).toList(),
        //       ),

        if(item.status!=2)
        Row(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            ElevatedButton(
                onPressed: () {
                  showApplyDialog(
                      context: context,
                      onSubmit: () {
                        viewModel.cancel(item.id!);
                      },
                      message: '确定删除该条订餐吗？');
                },
                child: Text('取消订餐'))
          ],
        )
      ],
    ).addToContainer(
        padding: EdgeInsets.all(10),
        decoration:
            BoxDecoration(color: Colors.white, borderRadius: 10.borderRadius));
  }

  @override
  EdgeInsets get listPadding =>
      EdgeInsets.only(left: 16, right: 16, bottom: 16);

  @override
  Widget get separatorDivider => Container(
        height: 12,
      );

  @override
  FoodOrderRecordViewModel createViewModel() {
    return FoodOrderRecordViewModel(context);
  }

  @override
  bool get canPullUp => true;
}
